package com.itzg.svn;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tigris.subversion.javahl.ClientException;
import org.tigris.subversion.javahl.SVNAdmin;

import com.itzg.fileutil.DirTreeFiller;

public class SvnSlammerLocal extends SvnSlammer {
	private static final Logger logger = LoggerFactory
			.getLogger(SvnSlammerLocal.class);

	private File localRepoDir;

	private String repoUri;

	public SvnSlammerLocal(SvnSlammerOptions options) {
		super(options);
		localRepoDir = options.getLocalRepoDir();
		if (localRepoDir == null) {
			throw new IllegalStateException("Missing local repo configuration");
		}
	}

	@Override
	public void setupRepo() {
		super.setupRepo();
		
		DirTreeFiller.deleteDir(localRepoDir);
		
		SVNAdmin svnadmin = new SVNAdmin();
		try {
			svnadmin.create(localRepoDir.getAbsolutePath(), false, false, null, SVNAdmin.FSFS);
			logger.info("Created repo at {}", localRepoDir);
		} catch (ClientException e) {
			logger.error("Trying to svadmin create {}", e, localRepoDir);
		}
		svnadmin.dispose();
	}

	@Override
	protected String getRepoBaseUrl() {
		if (repoUri == null) {
			repoUri = localRepoDir.toURI().toString();
			repoUri = repoUri.replaceFirst("file:/+", "file:///");
		}
		return repoUri;
	}
}
